using System.ServiceModel.Syndication;

namespace ServiceModel.Syndication.LEAP2A {
    /// <summary>
    /// An Atom link with additions for LEAP2A
    /// </summary>
    public class LeapCategory : SyndicationCategory {
        /// <summary>
        /// A reference to the main feed for access to the namespaces, needed to deal with CURIEs
        /// </summary>
        private readonly LeapFeed feed;

        /// <summary>
        /// Gets and sets the Schema for the category, CURIEs are accepted but will be converted to a full link.
        /// Comparisons should always use full links, CURIEs are only used when outputting the Atom feed.
        /// </summary>
        /// <value>A LEAP2A or Atom category scheme</value>
        /// <returns>The category scheme.</returns>
        public new string Scheme {
            get {
                if (base.Scheme != null) {
                    return feed.UriFromCurie(base.Scheme);
                }

                return null;
            }
            set {
                if (string.IsNullOrEmpty(value)) {
                    return;
                }

                string scheme = value;

                if (!scheme.Contains("://")) {
                    scheme = feed.LeapCategoryNamespace + scheme;
                }

                base.Scheme = feed.UriToCurie(scheme);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapCategory"/> class.
        /// </summary>
        /// <param name="feed">The LEAP2A feed.</param>
        /// <param name="name">The category term.</param>
        /// <param name="scheme">The category scheme.</param>
        /// <param name="label">The category label.</param>
        public LeapCategory(LeapFeed feed, string name, string scheme, string label)
            : base(name, scheme, label) {
            this.feed = feed;
            this.Scheme = scheme;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapLink"/> class.
        /// </summary>
        /// <param name="feed">The feed.</param>
        public LeapCategory(LeapFeed feed)
            : this(feed, null, null, null) {
        }
    }
}
